﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="MG.Core.js" />

$(function() {
    var drawOutput = function(html) {
        $('#output').hide().html(html);
        $('#output').slideDown(250, function() {
            $('#output').css('background-color', 'Yellow');
            setTimeout(function() {
                $('#output').css('background-color', '');
            }, 500);
        });
    };

    var getGroupHtml = function(group) {
        var sb = new MG.StringBuilder();
        sb.append('Version: ' + group.Version + '<br />');
        sb.append('UID: ' + group.UID + '<br />');
        sb.append('Name: ' + group.Name + '<br />');
        sb.append('Website Url: ' + group.WebsiteUrl + '<br />');
        sb.append('Logo Url: <img src="' + group.LogoUrl + '" alt="logo" /><br />');
        sb.append('Location: ' + group.LocationLatitude + ',' + group.LocationLatitude + '<br />');
        sb.append('Tags: ');
        for (var i = 0; i < group.Tags.length; i++) {
            sb.append(group.Tags[i] + ' ');
        }
        sb.append('<br />');
        sb.append('ICS Url: ' + group.IcsUrl + '<br />');
        sb.append('RSS Url: ' + group.RssUrl + '<br />');
        sb.append('Services Url: ' + group.ServicesUrl + '<br />');
        sb.append('Description: ' + group.Description + '<br />');
        sb.append('<br />');
        return sb.toString();
    };

    var loadRss = function() {
        var url = $('#rssFeedUrl').val();
        MG.loadRss(url, function(items) {
            var sb = new MG.StringBuilder('<h2>' + url + '</h2>');
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var publishDate = MG.getFormattedDate(MG.getDate(item.PublishDate));
                sb.append('<a href="' + item.Link + '">' +
                    item.Title + '</a><br />' + publishDate + '<br />');
                sb.append('<p>' + item.Summary + '</p><br />');
            }
            drawOutput(sb.toString());
        });
    };
    $('#loadRssButton').click(loadRss);

    var loadCalendar = function() {
        var url = $('#calendarFeedUrl').val();
        MG.loadCalendar(url, function(items) {
            var sb = new MG.StringBuilder('<h2>' + url + '</h2>');
            for (var i = 0; i < items.length; i++) {
                var item = items[i];
                var d = MG.getDate(item.StartDate);
                var startDate = MG.getFormattedDate(d);
                var startTime = MG.getFormattedTime(d);
                d = MG.getDate(item.EndDate);
                var endDate = MG.getFormattedDate(d);
                var endTime = MG.getFormattedTime(d);
                sb.append('Summary: ' + item.Summary + '<br />');
                sb.append('Start Date: ' + startDate + '<br />');
                sb.append('Start Time: ' + startTime + '<br />');
                sb.append('End Date: ' + endDate + '<br />');
                sb.append('End Time: ' + endTime + '<br />');
                sb.append('Description: ' + item.Description + '<br />');
                sb.append('Location: ' + item.Location + '<br />');
                sb.append('<br />');
            }
            drawOutput(sb.toString());
        });
    };
    $('#loadCalendarButton').click(loadCalendar);

    var loadGroup = function() {
        var url = $('#groupFeedUrl').val();
        MG.loadGroup(url, function(group) {
            var sb = new MG.StringBuilder('<h2>' + url + '</h2>');
            sb.append(getGroupHtml(group));
            drawOutput(sb.toString());
        });
    };
    $('#loadGroupButton').click(loadGroup);

    var loadGroups = function() {
        var url = $('#groupsFeedUrl').val();
        MG.loadGroups(url, function(groups) {
            var sb = new MG.StringBuilder('<h2>' + url + '</h2>');
            for (var i = 0; i < groups.length; i++) {
                var group = groups[i];
                sb.append(getGroupHtml(group));
            }
            drawOutput(sb.toString());
        });
    };
    $('#loadGroupsButton').click(loadGroups);
});
